#include "serverHead/threadPool.h" 
#include "serverHead/sys.h"
#include "serverHead/commands/command.h"

// 工人线程
void* workerThreadFunc(void *arg){
    threadPool_t *threadPool = (threadPool_t *)arg;;
    train_t* train = (train_t*)calloc(1, sizeof(train_t));   
    while(1){
        pthread_mutex_lock(&threadPool->taskQueue.mutex);
        while(threadPool->taskQueue.queueSize == 0){
            // 未接收到命令，且当前没有任务
            pthread_cond_wait(&threadPool->taskQueue.cond, &threadPool->taskQueue.mutex);
        }
        //正常执行
        
        printf("工人线程得到任务！\n");
        workerExecute(threadPool->taskQueue.pFront->train, threadPool->taskQueue.pFront->info);
        sendTrain(threadPool->taskQueue.pFront->info->netfd, threadPool->taskQueue.pFront->train);
        taskDeQueue(&threadPool->taskQueue);
        pthread_mutex_unlock(&threadPool->taskQueue.mutex);
    }
}

int makeWorker(threadPool_t *threadPool){ 
    for(int i = 0; i < threadPool->threadnum; ++i){
        pthread_create(&threadPool->tidArr[i], NULL, workerThreadFunc, (void*)threadPool);
    }
    return 0;
}

int workerExecute(train_t* train, userInfo* info){
    switch(train->order){
    case ORDER_CD:
        printf("123\n");
        myCd(train, info->my_pathname);
        break;
    case ORDER_LS:
        printf("ls!\n");
        break;
    case ORDER_PUTS:
        printf("puts\n");
        break;
    case ORDER_GETS:
        printf("gets!\n");
        break;
    case ORDER_REMOVE:
        printf("remove!\n");
        break;
    case ORDER_PWD:
        printf("pwd!\n");
        break;
    case ORDER_MKDIR:
        printf("mkdir!\n");
        break;
    case ORDER_RMDIR:
        printf("rmdir!\n");
        break;
    default:
        printf("error!\n");
        break;
    }
    return 0;
}
